package net.xdclass.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.component.SmsComponent;
import net.xdclass.config.SmsConfig;
import net.xdclass.constant.RedisKey;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.service.NotifyService;
import net.xdclass.util.CheckUtil;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class NotifyServiceImpl implements NotifyService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SmsComponent smsComponent;
    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 十分钟过期时间
     *
     */
    private static final long TTL = 60 * 1000 * 10;


    /**
     * 发送短信验证码
     * @param sendCodeEnum
     * @param to
     * @return
     */
    @Override
    public JsonData sendCode(SendCodeEnum sendCodeEnum, String to) {
        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY,sendCodeEnum.name(),to);
        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        //如果不为空，判断是否超过60秒 xxxx_1644651217454时间戳
        if(StringUtils.isNotBlank(cacheValue)){
            String[] split = cacheValue.split("_");
            long ttl = Long.parseLong(split[1]);
            //当前时间戳-缓存时间戳
            long currentTime = System.currentTimeMillis();
            if(currentTime-ttl < 60 * 1000){
                //重复发送
                log.info("重复发送短信验证码,时间间隔：{}秒",currentTime-ttl);
                return JsonData.buildResult(BizCodeEnum.CODE_LIMITED);
            }
        }
        //生成拼接验证码
        String code = CommonUtil.getRandomCode(6);
        String value = code + "_" + CommonUtil.getCurrentTimestamp();
        redisTemplate.opsForValue().set(cacheKey,value,TTL,TimeUnit.MILLISECONDS);
        if(CheckUtil.isEmail(to)){
            //发送邮件
        } else if (CheckUtil.isPhone(to)) {
            //发送短信
            smsComponent.send(to,smsConfig.getTemplateId(),code);
        }
        return JsonData.buildSuccess();

    }

    @Override
    public boolean checkCode(SendCodeEnum sendCodeEnum, String to, String code) {
        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY,sendCodeEnum.name(),to);
        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        if(StringUtils.isNotBlank(cacheValue)){
            //切割xxxx_1644651217454时间戳
            String[] split = cacheValue.split("_");
            String cacheCode = split[0];
            if(cacheCode.equals(code)){
                //校验成功，删除缓存
                redisTemplate.delete(cacheKey);
                return true;
            }
        }
        return false;
    }
}
